1. Field of the Art
The present disclosure relates generally to the field of data processing in prototype based dynamic languages. Specifically presented are methods and systems for declaring and using properties in automatically-added property classes in JavaScript® and other ECMAScript-compatible languages.
2. Description of the Related Art
Computer programming languages have developed rapidly over the years. Modern computer languages typically include provisions for declaring classes, from which objects are instantiated. This allows object-oriented approaches to programming. Object-oriented programming commonly allows encapsulation of code so that the code is modular and reusable for other programs as well as easier to abstract in regard to real-world things.
Modern computer languages that support object-oriented programming typically support inheritance between classes. A sub-class inherits methods (i.e., functions) and properties (i.e., attributes) from a parent class. For example, a rectangle class can inherit a LineThickness property from a parent shape class, the classes being used to draw graphical objects on a display.
Although many modern programming languages are designed to be compiled before they execute, other modern programming languages are interpreted on the fly during program execution. Some of the interpreted languages are “dynamic programming languages” in that their source code can be added to, objects and definitions can be extended, and/or their type systems can be modified during program execution (i.e., at runtime). The JavaScript® programming language is one example of a dynamic programming language. JavaScript is sometimes referred to as a dialect of ECMAScript.
ECMAScript is a computer programming scripting language complying with Ecma International's ECMA-262 specification and International Organization for Standardization (ISO)/International Electrotechnical Commission (IEC) 16262. ECMA was formerly known as the European Computer Manufacturers Association (ECMA). ECMAScript implementations include Netscape/Sun/Oracle JavaScript, Microsoft JScript, and Adobe ActionScript.
Dynamic programming languages commonly perform type checking at runtime. That is, the assignment of constants or variables to other variables, the passing of arguments to functions, etc. are checked to ensure that whatever is being assigned is compatible with the data type of the variable to which it is being assigned. For example, the number “3” is compatible to be stored in an integer variable, but it may not be compatible with a Boolean variable. If the number “3” were to be assigned to a Boolean variable in a strongly typed language, a type mismatch error can occur.
Some programming languages, such as JavaScript, attempt to convert the offending variable to the data type of the target variable. For example, the number “3” can be converted to a “true” (because it is non-zero) and then stored in the target Boolean variable. Although this automatic, behind-the-scenes conversion can prevent a type mismatch error, it can hide problems when programmers do not intend the mismatch. Thus, the benefits of having fewer runtime errors can weigh against the difficulties of debugging the code during development.
Along with relaxed type control, JavaScript does not prevent a programmer from overwriting a declared variable or object of one type with a variable or object of another type. For example, if a variable is declared as
var x=5;
and then later on in the code the variable is re-declared as:
var x=“cat”;
there is no error. That is, there is no error in re-declaring the same variable later on in the code, even if the newly assigned value is of a completely different data type. JavaScript essentially ignores (and overwrites) the previous declaration. Although this prevents errors from popping up upon the re-declaration, it can be much more difficult for a programmer to debug code in which a variable or object is inadvertently re-declared. This can be especially troublesome for complex objects, such as graphical objects, in which dozens, hundreds, or thousands of properties must be tracked and managed.
There exists a need in the art for better techniques to program in widely supported, object-oriented, dynamic programming languages such as JavaScript.